
#include "eye_tiling.h"
#include "register/op_def_registry.h"

#include "tiling/platform/platform_ascendc.h"

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

    EyeTilingData tiling;
    uint32_t sizeofdatatype;
    uint32_t totalLengthAligned;
  
    // 1. 平台信息
    // uint64_t ub_size;
    // auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    // ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
  
    // 2. 获取数据信息
    uint32_t totalLength = context->GetInputTensor(0)->GetShapeSize();
    // sizeofdatatype = GetSizeByDataType(context->GetInputDesc(0)->GetDataType());
  
    const int64_t* pnum_rows = context->GetAttrs()->GetInt(0);
    const int64_t* pnum_columns = context->GetAttrs()->GetInt(1);
  
    int32_t num_rows = *pnum_rows;
    int32_t num_columns = *pnum_columns;


    tiling.set_num_columns(num_columns);
    tiling.set_num_rows(num_rows);

    int32_t batchSize = num_columns * num_rows;
    int32_t batchNum = totalLength / batchSize;

    tiling.set_batchNum(batchNum);
    int aivNum = 1;
    if (batchNum < 40){
      aivNum = batchNum;
    } else {
      aivNum = 40;
    }
    context->SetBlockDim(aivNum);  
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Eye : public OpDef {
public:
    explicit Eye(const char* name) : OpDef(name)
    {
        this->Input("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_DOUBLE})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("num_rows").Int();
        this->Attr("num_columns").AttrType(OPTIONAL).Int(0);
        this->Attr("batch_shape").AttrType(OPTIONAL).ListInt({});
        this->Attr("dtype").AttrType(OPTIONAL).Int(0);

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");

    }
};

OP_ADD(Eye);
}
